#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> ret;
        sort(nums.begin(), nums.end());
        negate<int> neg;
        for (int i = 0; i < nums.size(); i++) {
            int left = i + 1;
            int right = nums.size() - 1;
            while (left < right) {
                vector<int> t;
                if (nums[left] + nums[right] > neg(nums[i])) {
                    right--;
                } else if (nums[left] + nums[right] < neg(nums[i])) {
                    left++;
                } else {
                    while (nums[left] == nums[left + 1] && left >= right) {
                        left++;
                    }
                    while (nums[right] == nums[right - 1] && left >= right) {
                        right--;
                    }
                    t.push_back(nums[i]);
                    t.push_back(nums[left]);
                    t.push_back(nums[right]);
                    ret.push_back(t);
                    left++;
                    right--;
                }
            }
            if (nums[i] == nums[i + 1]) {
                if (i >= nums.size()) {
                    break;
                }
                i++;
            }
        }
        return ret;
    }
};

int main()
{
    vector<vector<int>> vv;
    Solution s;
    vector<int> v{1,2,-2,-1};
    vv = s.threeSum(v);
    return 0;
}